package com.hrm.attendance.controller;

import com.hrm.attendance.service.ArchiveService;
import com.hrm.attendance.service.AtteService;
import com.hrm.attendance.service.ExcelImportService;
import com.hrm.common.controller.BaseController;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.domain.attendance.entity.ArchiveMonthly;
import com.hrm.domain.attendance.entity.ArchiveMonthlyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attendances")
public class AttendanceController extends BaseController {

    @Autowired
    private ExcelImportService excelImportService;
    @Autowired
    private AtteService atteService;
    @Autowired
    private ArchiveService archiveService;

    /**
     * 上传考勤数据
     * @param file
     * @return
     */
    @RequestMapping(value = "/import",method = RequestMethod.POST)
    public Result importExcel(@RequestParam(name = "file") MultipartFile file) throws Exception {
        excelImportService.importAttendanceExcel(file,companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
        page=1&pagesize=10
        返回值： data（用户信息、list【考勤列】）、monthOfReport、tobeTaskCount
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result getAtteData(int page,int pageSize) throws ParseException {
        Map<String,Object> map = atteService.getAtteData(companyId,page,pageSize);
        return new Result(ResultCode.SUCCESS,map);
    }

    /**
        获取月报表归档数据
        /report?atteDate=201907
     */
    @RequestMapping(value = "/report",method = RequestMethod.GET)
    public Result getReport(String atteDate) {
        List<ArchiveMonthlyInfo> list = atteService.getReport(atteDate, companyId);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     新建报表
     /newReport?atteDate=201907&yearMonth=201908
     */
    @RequestMapping(value = "/newReport",method = RequestMethod.GET)
    public Result newReport(String yearMonth) {
        atteService.newReport(yearMonth,companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
         月报表数据归档
            /archive/item?archiveDate=201907
     */
    @RequestMapping(value = "/archive/item",method = RequestMethod.GET)
    public Result archive(String archiveDate) {
        archiveService.saveArchive(archiveDate,companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     当前年历史归档考勤数据查看
     /report/year=2019
     前端中按年月列表展示
     */
    @RequestMapping(value = "/report",method = RequestMethod.GET)
    public Result yearReport(String year) {
        List<ArchiveMonthly> list = archiveService.getYearReport(year,companyId);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     月历史考勤数据列表查看
     /report/{yearMonth}
     */
    @RequestMapping(value = "/report/{yearMonth}",method = RequestMethod.GET)
    public Result reportInfo(@PathVariable String yearMonth) {
        List<ArchiveMonthlyInfo> list = archiveService.getReportInfo(yearMonth, companyId);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     根据用户id和月份查询已经归档的考勤明细
     /archive/{userId}/{yearMonth}
     */
    @RequestMapping(value = "/archive/{userId}/{yearMonth}",method = RequestMethod.GET)
    public Result historyData(@PathVariable String userId,@PathVariable String yearMonth) {
        ArchiveMonthlyInfo archiveMonthlyInfo = archiveService.findUserIdArchiveDetail(userId,yearMonth);
        return new Result(ResultCode.SUCCESS,archiveMonthlyInfo);
    }

}
